<template>
  <div>
    <div class="returnBtn">
      <t-button icon="arrow-left" @click="cancel">取消</t-button>
    </div>
    <div class="content">
      <t-form ref="data" :model="formValidate" :rules="ruleValidate" :label-width="80">
        <div class="row">
          <div class="col-6 p-r-15">
            <t-form-item label="数据权限名称" prop="name">
              <t-input v-model="formValidate.name" placeholder="请输入..."></t-input>
            </t-form-item>
          </div>
        </div>
        <div class="row">
          <div class="col-6">
            <t-form-item label="业务对象" prop="boObject">
              <t-select v-model="formValidate.boObject" placeholder="请选择" clearable>
                <t-option v-for="(item, index) in boList" :key="index" :value="item.text">
                  {{ item.text }}
                </t-option>
              </t-select>
            </t-form-item>
          </div>
        </div>
        <div class="row">
          <div class="col-6">
            <t-form-item label="业务组件" prop="boComponent">
              <t-select v-model="formValidate.boComponent" placeholder="请选择" clearable>
                <t-option v-for="(item, index) in bobcList" :key="index" :value="item.BC_TABLE0">
                  {{ item.BC_TABLE0 }}
                </t-option>
              </t-select>
            </t-form-item>
          </div>
        </div>
        <div class="row">
          <div class="col-12">
            <t-form-item label="备注">
              <t-input v-model="formValidate.remarks" :rows="10" spellcheck = "false" type="textarea" placeholder="请输入"></t-input>
            </t-form-item>
          </div>
        </div>
      </t-form>
      <div class="btns">
        <div>
          <t-button @click="cancel">取消</t-button><t-button type="primary" style="margin-left:14px" @click="preservation">保存</t-button>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data () {
    const VALIDATEPASS = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('排序不能为空'))
      } else {
        if (!/^[0-9]*[1-9][0-9]*$/.test(value)) {
          callback(new Error('请输入正整数'))
        }
        callback()
      }
    }
    return {
      boList: [],
      bobcList: [],
      formValidate: {
        name: '',
        moduleCode: '',
        boObject: '',
        boComponent: '',
        remarks: ''
      },
      ruleValidate: {
        sort: [
          {
            validator: VALIDATEPASS,
            trigger: 'blur'
          }
        ],
        name: [
          {required: true, message: '菜单名称不能为空', trigger: 'blur'}
        ],
        boObject: [
          {required: true, message: '业务对象不能为空', trigger: 'blur'}
        ],
        boComponent: [
          {required: true, message: '业务组件不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  computed: {
    boObject() {
      return this.formValidate.boObject
    }
  },
  watch: {
    boObject(newValue) {
      console.log('----new>>>>', newValue)
      this._getBoBcList(newValue)
    }
  },
  methods: {
    // 表单重置
    onReset() {
      this.$refs['data'].resetFields()
    },
    // 保存
    preservation () {
      this.$refs['data'].validate(valid => {
        if (valid) {
          let list = {}
          let datas = {}
          list.bo = 'user'
          list.bc = 'USER_FUNCTION'
          list.validation_method = 'com.wframe.usermanager.services.valid.FuncMuneCreateValidator'
          datas.FUNC_TYPE = 'D'
          datas.MODULE_CODE = this.$route.params.id
          datas.NAME = this.formValidate.name
          datas.FUNC_IMG = this.formValidate.boObject
          datas.FUNC_ARG = this.formValidate.boComponent
          datas.REMARK = this.formValidate.remarks
          list.ins_value = JSON.stringify(datas)
          this.http.$http.post(this.services.USERMNG_CREATE, list)
            .then(res => {
              if (res.data.CODE === 0) {
                this.onReset()
                this.reset()
                this.$Message.success('新建数据权限成功!')
                this.$router.push('/auth/')
              } else {
                this.$Message.danger('新建数据权限失败!')
              }
            })
        } 
      })
    },
    reset () {
      this.formValidate.name = ''
      this.formValidate.moduleCode = ''
      this.formValidate.boObject = ''
      this.formValidate.boComponent = ''
      this.formValidate.remarks = ''
    },
    cancel () {
      this.$router.push('/auth')
    },
    _getBoNameList() {
      this.http.$http.post(this.services.USERMNG_PROCESS, {
        init_method: 'com.wframe.base.serivces.QueryBoNameList'
      }).then(res => {
        console.log('---_getBoNameList>>>>', res)
        if (res.data && res.data.CODE === 0) {
          this.boList = res.data.RESULT
        } else {
          this.$Message.danger('查询业务对象失败!')
        }
      })
    },
    _getBoBcList(value) {
      console.log('---------value>>>>>>>', value)
      if (value != '' && value != null && value != undefined) {
        this.http.$http.post(this.services.USERMNG_PROCESS, {
          bo: value,
          init_method: 'com.wframe.base.serivces.QueryBOBCList'
        }).then(res => {
          console.log('---_getBoBcList>>>>', res)
          if (res.data && res.data.CODE === 0) {
            this.bobcList = res.data.RESULT.BC_LIST
          } else {
            this.$Message.danger('查询业务组件失败!')
          }
        })
      }
    },
  },
  created() {
    this._getBoNameList() // 数据权限业务对象查询接口
    this._getBoBcList() // 数据权限业务组件查询接口
  }
}
</script>
<style scoped lang="less">
.returnBtn{
  margin: 30px 0;
}
.content{
  max-width: 590px;
  margin: 0 auto 128px;
  .col-6,
  .col-12{
    padding: 0;
  }
  .p-r-15{
    padding-right: 15px;
  }
  .p-l-15{
    padding-left: 15px;
  }
  .btns{
    // display: inline-block;
    max-width: 170px;
    margin: 0 auto;
  }
}
.type{
  position: relative;
  span {
    position: absolute;
    left: 82px;
    top: -27px;
    padding-left: 5px;
    color: red;
  }
  .feedback{
    .icon{
      display: inline-block;
      width: 12px;
      height: 12px;
      color: red;
      font-size: 12px;
      margin-right: .3em;
    }
  }
}
</style>
